****APPENDIX FIGURE F3****

cd "$main_dir/output/figures/appendix_f"

graph set window fontface "Palatino"

global basic 	sex marital religion edu

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/harmonized_dataset.dta"

merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/growth_rate_mpd_var_exp.dta", keepusing(total_growth_rate_obs yob_ln_gdppc) nogen
merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/growth_mpd_var_lambda_exp.dta", nogen

fegen subregion_yob = group(subregion yob) if yob!=-99
fegen svy_survey_year = group(survey_year survey)

cap drop sample
gen sample = 1 if born_country!=0 & total_growth_rate_obs>=0.9*(age-1) & total_growth_rate_obs!=.

//--------------------------------------------------------
//Run grid search
//--------------------------------------------------------

mat results = J(61, 3, .)
mat colnames results = lambda beta rss

local i = 1
local j = -3
local varlist wgrowth_rate_neg3 wgrowth_rate_neg2_9 wgrowth_rate_neg2_8 wgrowth_rate_neg2_7 wgrowth_rate_neg2_6 wgrowth_rate_neg2_5 wgrowth_rate_neg2_4 wgrowth_rate_neg2_3 wgrowth_rate_neg2_2 wgrowth_rate_neg2_1 wgrowth_rate_neg2 wgrowth_rate_neg1_9 wgrowth_rate_neg1_8 wgrowth_rate_neg1_7 wgrowth_rate_neg1_6 wgrowth_rate_neg1_5 wgrowth_rate_neg1_4 wgrowth_rate_neg1_3 wgrowth_rate_neg1_2 wgrowth_rate_neg1_1 wgrowth_rate_neg1 wgrowth_rate_neg0_9 wgrowth_rate_neg0_8 wgrowth_rate_neg0_7 wgrowth_rate_neg0_6 wgrowth_rate_neg0_5 wgrowth_rate_neg0_4 wgrowth_rate_neg0_3 wgrowth_rate_neg0_2 wgrowth_rate_neg0_1 wgrowth_rate_0 wgrowth_rate_0_1 wgrowth_rate_0_2 wgrowth_rate_0_3 wgrowth_rate_0_4 wgrowth_rate_0_5 wgrowth_rate_0_6 wgrowth_rate_0_7 wgrowth_rate_0_8 wgrowth_rate_0_9 wgrowth_rate_1 wgrowth_rate_1_1 wgrowth_rate_1_2 wgrowth_rate_1_3 wgrowth_rate_1_4 wgrowth_rate_1_5 wgrowth_rate_1_6 wgrowth_rate_1_7 wgrowth_rate_1_8 wgrowth_rate_1_9 wgrowth_rate_2 wgrowth_rate_2_1 wgrowth_rate_2_2 wgrowth_rate_2_3 wgrowth_rate_2_4 wgrowth_rate_2_5 wgrowth_rate_2_6 wgrowth_rate_2_7 wgrowth_rate_2_8 wgrowth_rate_2_9 wgrowth_rate_3
foreach var of local varlist {
	
	qui reghdfe trust_govt `var' yob_ln_gdppc if sample==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
	cap drop z*
	local list trust_govt `var' yob_ln_gdppc
	foreach x of local list {
		qui egen z`x' = std(`x') if e(sample)==1
	}
	
	qui reghdfe ztrust_govt z`var' zyob_ln_gdppc if e(sample)==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
	mat results[`i', 1] = `j'
	mat results[`i', 2] = e(b)[1,1]
	mat results[`i', 3] = e(rss)
	local ++i
	local j = `j' + 0.1
	
	di "`var' done"
}

matrix list results
svmat double results, names(col)
keep lambda beta rss
drop if lambda==.

//--------------------------------------------------------
//Create Figure F3
//--------------------------------------------------------									

scatter rss lambda, graphregion(color(white)) bgcolor(white) title("RSS", color(black)) ///
	ylabel(,labs(small) format(%9.2f)) xlabel(,labs(small)) xtitle("{&lambda}", margin(small)) ytitle("RSS", margin(small)) ///
	note("Note: each point represents a separate regression RSS per value of {&lambda} used to calculate growth experience weights. The grid search" "ranges from -3 to 3 over increments of 0.1.", size(vsmall))
	graph export "figure_f3_a.png", replace
															
scatter beta lambda, graphregion(color(white)) bgcolor(white) title("Coefficients", color(black)) ///
	ylabel(,labs(small) format(%9.2f)) xlabel(,labs(small)) xtitle("{&lambda}", margin(small)) ytitle("{&beta}", margin(small)) ///
	note("Note: each point represents a separate regression {&beta} per value of {&lambda} used to calculate growth experience weights. The grid search" "ranges from -3 to 3 over increments of 0.1.", size(vsmall))
	graph export "figure_f3_b.png", replace
	
